/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.SanPham;
import util.HibernateUtil;

public class SanPhamDAO {
    public static SanPham layThongTinSanPham(String maSanPham){
        SanPham s=null;
         Session session = HibernateUtil.getSessionFactory() .openSession();
        String hql="Select s from SanPham s where s.maSanPham=:maSanPham";
         Query query=session.createQuery(hql);
         query.setString("maSanPham", maSanPham);
          Object obj=query.uniqueResult();
            s=(SanPham) obj;
            if( obj!=null){
                s.getClass();
            }
         session.close();
         return s;       
    }
    public static List<SanPham> timSanPhamTheoTen(String tenSanPham){
        List s=null;
         Session session = HibernateUtil.getSessionFactory() .openSession();
        String hql="Select s from SanPham where s.tenSanPham=:tenSanPham";
         Query query=session.createQuery(hql);
         query.setString("tenSanPham",tenSanPham);
          Object obj=query.uniqueResult();
            s=(List<SanPham>) obj;
            if((List<SanPham>) obj!=null){
                s.getClass();
            }
         session.close();
         return s;       
    }
    public static List<SanPham> laDanhSachSanPham(String maDanhMuc){
         List<SanPham> ds = null; 
        Session session = HibernateUtil.getSessionFactory() .openSession(); 
        try { 
            String hql = "select sv from ThuongHieu sv where sv.thuongHieu.maThuongHieu=:ma";
            Query query=session.createQuery(hql);
         query.setString("maThuongHieu", maDanhMuc);
            Object obj=query.uniqueResult();
            ds=(List<SanPham>) obj;
        } catch (HibernateException ex) { 
            //Log the exception 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return ds; 
    }
     public static List<SanPham> layDanhSachSanPham(int maThuongHieu,int soLuongSanPhamTrenTrang,int trang){
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<SanPham> list;
        String hql = "Select s from SanPham s where s.thuongHieu.maThuongHieu ='"+maThuongHieu+"'";
        Query query = session.createQuery(hql);
        query.setFirstResult((trang-1)*soLuongSanPhamTrenTrang);
        query.setMaxResults(soLuongSanPhamTrenTrang);
        list = query.list();
        session.close();
        return list;
     }
     
     public static List<SanPham> layDanhSachSanPham(){
         List<SanPham> ds = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
         String hql="select sp from SanPham sp order by sp.size asc";
         Query query =session.createQuery(hql);
         ds=query.list();
         session.close();
         return ds;
     }
     
     public static List<SanPham> timSanPham(String maThuongHieu, String maLoai, String size, String giaBan){
         List<SanPham> ds=null;
         Session session =HibernateUtil.getSessionFactory().openSession();
         String hql="select sp from SanPham sp ";
         if(maThuongHieu.equals("")==false){
                hql = hql + " where  sp.thuongHieu.maThuongHieu = :maThuongHieu ";
        }
         if(maLoai.equals("")==false){
             hql = hql  + " or sp.loaiSanPham.maLoaiSanPham = :maLoai ";
         }
         if(size.equals("")==false){
             hql=hql+" or sp.size = :size";
         }
        if(giaBan.equals("")==false){
             if(giaBan.equals(">500000")){
                 giaBan="500000";
                 hql=hql+" or sp.giaBan >= :giaBan";
             }
              hql=hql+" or sp.giaBan <= :giaBan";
         }
         //                      + "or sp.size =15 or sp.loaiSanPham.maLoaiSanPham ='L002' or sp.giaBan <150""
         Query query =session.createQuery(hql);
         query.setInteger("maThuongHieu", Integer.parseInt(maThuongHieu));
         query.setString("maLoai", maLoai);
         query.setInteger("size", Integer.parseInt(size));
         query.setDouble("giaBan", Double.parseDouble(giaBan));
         ds=query.list();
         session.close();
         return ds;
     }
     public static List<SanPham> timSanPham(String maThuongHieu, String maLoai, String size, String giaBan, int  trang, int soLuongSachTrenTrang){
         List<SanPham> ds=null;
         Session session =HibernateUtil.getSessionFactory().openSession();
         String hql="select sp from SanPham sp ";
         if(maThuongHieu.equals("")==false){
                hql = hql + " where  sp.thuongHieu.maThuongHieu = :maThuongHieu ";
        }
         if(maLoai.equals("")==false){
             hql = hql  + " or sp.loaiSanPham.maLoaiSanPham = :maLoai ";
         }
         if(size.equals("")==false){
             hql=hql+" or sp.size = :size";
         }
          if(giaBan.equals("")==false){
             if(giaBan.equals(">500000")){
                 giaBan="500000";
                 hql=hql+" or sp.giaBan >= :giaBan";
             }
              hql=hql+" or sp.giaBan <= :giaBan";
         }
         //                      + "or sp.size =15 or sp.loaiSanPham.maLoaiSanPham ='L002' or sp.giaBan <150""
         int n=(trang-1)*soLuongSachTrenTrang;
         int m=soLuongSachTrenTrang;
         Query query =session.createQuery(hql);
         query.setInteger("maThuongHieu", Integer.parseInt(maThuongHieu));
         query.setString("maLoai", maLoai);
         query.setInteger("size", Integer.parseInt(size));
         query.setDouble("giaBan", Double.parseDouble(giaBan));
         query.setFirstResult((trang-1)*soLuongSachTrenTrang);
        query.setMaxResults(soLuongSachTrenTrang);
         ds=query.list();
         session.close();
         return ds;
     }
      public static List<SanPham> layDanhSachSanPhamTheoLoai(String maLoaiSanPham,int soLuongSanPhamTrenTrang,int trang ){
          Session session = HibernateUtil.getSessionFactory().openSession();
        List<SanPham> list;
        String hql = "Select s from SanPham s where s.loaiSanPham.maLoaiSanPham ='"+maLoaiSanPham+"'";
        Query query = session.createQuery(hql);
        query.setFirstResult((trang-1)*soLuongSanPhamTrenTrang);
        query.setMaxResults(soLuongSanPhamTrenTrang);
        list = query.list();
        session.close();
        return list;
     }
      
      public static int tinhSoTrang(String maThuongHieu, String maLoai, String size, String giaBan, int soLuongSanPhamTrenTrang){
          int soTrang=0;
          int soLuong =0;
          List<SanPham> ds=null;
         Session session =HibernateUtil.getSessionFactory().openSession();
         String hql="select sp from SanPham sp ";
         if(maThuongHieu.equals("")==false){
                hql = hql + " where  sp.thuongHieu.maThuongHieu = :maThuongHieu ";
        }
         if(maLoai.equals("")==false){
             hql = hql  + " or sp.loaiSanPham.maLoaiSanPham = :maLoai ";
         }
         if(size.equals("")==false){
             hql=hql+" or sp.size = :size";
         }
          if(giaBan.equals("")==false){
             if(giaBan.equals(">500000")){
                 giaBan="500000";
                 hql=hql+" or sp.giaBan >= :giaBan";
             }
              hql=hql+" or sp.giaBan <= :giaBan";
         }
         //                      + "or sp.size =15 or sp.loaiSanPham.maLoaiSanPham ='L002' or sp.giaBan <150""
         Query query =session.createQuery(hql);
         query.setInteger("maThuongHieu", Integer.parseInt(maThuongHieu));
         query.setString("maLoai", maLoai);
         query.setInteger("size", Integer.parseInt(size));
         query.setDouble("giaBan", Double.parseDouble(giaBan));
         ds=query.list();
         session.close();
         soLuong=ds.size();
          if (soLuongSanPhamTrenTrang==-1) {
              soTrang=1;
          }
          else{
              soTrang=soLuong/soLuongSanPhamTrenTrang;
              if(soLuong%soLuongSanPhamTrenTrang!=0){
                  soTrang++;
              }
          }
          return soTrang;
      }
}
